package cn.zxy.tokenx.config;

import cn.hutool.core.util.StrUtil;
import cn.zxy.tokenx.interceptor.TokenInterceptor;
import cn.zxy.tokenx.properties.TokenXProperties;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

//@Configuration
@Import(TokenXProperties.class)
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private TokenXProperties tokenXProperties;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        TokenInterceptor tokenInterceptor = new TokenInterceptor(tokenXProperties.getKey());

        String excludePathPatterns = tokenXProperties.getExcludePathPatterns();
        String[] excludePathArrays = StrUtil.splitToArray(excludePathPatterns, ",");


//        链式调用
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns(tokenXProperties.getPathPatterns())
                .excludePathPatterns(excludePathArrays)//白名单
                .order(tokenXProperties.getOrder());//数字越小，越先执行
    }
}
